#include <iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


class Solution{
private:
	bool compare(TreeNode* left,TreeNode* right){
		if(left != nullptr && right == nullptr){
			return false;
		}else if(left == nullptr && right != nullptr){
			return false;
		}else if(left == nullptr && right == nullptr){
			return true;
		}else if(left->val != right->val){
			return false;
		}
		int outSame = compare(left->left,right->right);
		int inSame = compare(left->right,right->left);
		return outSame && inSame;
	}
public:
	bool isSymmetric(TreeNode* root) {
		if(root == nullptr){
			return false;
		}
		return compare(root->left,root->right);
	}
};
